package com.mezz.bones.framework.base.domain.response;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.mezz.bones.framework.base.domain.enums.ResponseCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

@Data
@ApiModel(value = "接口返回对象", description = "接口返回对象")
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 返回处理消息
     */
    @ApiModelProperty(value = "返回处理消息")
    private String message = ResponseCode.SUCCESS.getMessage();

    /**
     * 返回代码
     */
    @ApiModelProperty(value = "返回代码")
    private String code = ResponseCode.SUCCESS.getCode();

    /**
     * 返回数据对象 data
     */
    @ApiModelProperty(value = "返回数据对象")
    private T data;

    @ApiModelProperty(value = "扩展属性")
    private Object extend;

    /**
     * 时间戳
     */
    @ApiModelProperty(value = "时间戳")
    private Long timestamp = System.currentTimeMillis();

    public Result() {
    }

    /**
     * 兼容VUE3版token失效不跳转登录页面
     *
     * @param code
     * @param message
     */
    public Result(String code, String message) {
        this.code = code;
        this.message = message;
    }

    public static <T> Result<T> error(String code, String msg, T data) {
        Result<T> r = new Result<T>();
        r.setCode(code);
        r.setMessage(msg);
        r.setData(data);
        return r;
    }

    public static <T> Result<T> error(String code, String msg) {
        return error(code,msg,null);
    }

    public static <T> Result<T> sysErrorWithCode(ResponseCode responseCode) {
        return error(responseCode.getCode(), StrUtil.format("系统繁忙[{}],请稍后再试!",responseCode.getCode()));
    }

    public boolean isOk(){
        return this.code.equals(ResponseCode.SUCCESS.getCode());
    }

    public static <T> Result<T> error(String msg) {
        Result<T> r = new Result<T>();
        r.setCode(ResponseCode.SYS_ERROR.getCode());
        r.setMessage(msg);
        return r;
    }

    public Result<T> success(String message) {
        this.message = message;
        return this;
    }

    public static <T> Result<T> OK() {
        return new Result<T>();
    }

    public static <T> Result<T> OK_MESSAGE(String msg) {
        Result<T> r = new Result<T>();
        r.setMessage(msg);
        return r;
    }

    public static <T> Result<T> OK(T data) {
        Result<T> r = new Result<T>();
        r.setData(data);
        return r;
    }



    public static <T> Result<T> OK(String msg, T data) {
        Result<T> r = new Result<T>();
        r.setMessage(msg);
        r.setData(data);
        return r;
    }

    public static <T> Result<T> error(ResponseCode codeEnum, String msg) {
        Result<T> r = new Result<T>();
        r.setCode(codeEnum.getCode());
        r.setMessage(msg);
        return r;
    }

    public static <T> Result<T> error(ResponseCode codeEnum) {
        Result<T> r = new Result<T>();
        r.setCode(codeEnum.getCode());
        r.setMessage(codeEnum.getMessage());
        return r;
    }

    public static <T> Result<T> error(ResponseCode codeEnum, T data, JSONObject extend) {
        Result<T> r = new Result<T>();
        r.setCode(codeEnum.getCode());
        r.setMessage(codeEnum.getMessage());
        r.setData(data);
        r.setExtend(extend);
        return r;
    }

    public static <T> Result<T> error(ResponseCode codeEnum, T data) {

        return error(codeEnum,data,null);
    }

    public static <T> Result<T> data(String code, T data) {
        Result<T> r = new Result<T>();
        r.setCode(code);
        r.setData(data);
        return r;
    }

    /**
     * 设置扩展属性信息
     *
     * @param extend
     * @return
     */
    public Result<T> extend(Object extend){
        this.extend = extend;
        return this;
    }

}
